// {{{ imports
import java.awt.Panel;
import java.awt.Frame;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.net.HttpURLConnection;
import java.net.URLConnection;
import java.net.URL;

import org.python.util.PythonInterpreter;
import org.python.core.*;

import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.geometry.ColorCube;
import javax.media.j3d.BranchGroup;
// }}}

public class JythonApplet extends JApplet {
	//Called when this applet is loaded into the browser.
	public void init() {
		System.out.println("JythonApplet init");
		this.add(new JLabel("Applet-init"));
		this.setVisible(true);
		checkJython(this, getParameter("JYTHONAPPLETPATH"));
	}
	public static void checkJython(Panel apanel, String jythonappletpath) {
		PythonInterpreter interp = new PythonInterpreter();
		interp.set("apanel", apanel);
		try {
			//URLConnection urlc = new URL("file:///home/local_admin/Projects/EasyApplets/src/JythonApplet.py").openConnection();
			//URLConnection urlc = new URL("http://www.ebi.ac.uk/~swanand/JA/EasyApplets/src/JythonApplet.py").openConnection();
			URLConnection urlc = new URL(jythonappletpath).openConnection();
			urlc.setUseCaches(false);
			InputStream is = new BufferedInputStream(urlc.getInputStream());
			interp.execfile(is);
			interp.cleanup(); is.close();
		}
		catch(Exception e) { System.out.println("Error interpreting URL : " + e); } 
	}
	public static void checkJythonStandalone() {
		Panel jycontainer = new Panel();
		checkJython(jycontainer, System.getProperty("JYTHONAPPLETPATH"));
		Frame fr = new Frame();
		fr.add(jycontainer, BorderLayout.CENTER);
		fr.setSize(300,300);
		fr.setVisible(true);
	}
	public static void checkJava3d() {
		SimpleUniverse universe = new SimpleUniverse();
		BranchGroup group = new BranchGroup();
		group.addChild(new ColorCube(0.3));
		universe.getViewingPlatform().setNominalViewingTransform();
		universe.addBranchGraph(group);
	}
	public static void main(String[] argv) {
		System.out.println("JythonApplet main");
		//checkJava3d();
		checkJythonStandalone();
	}
}
